Ext.define('AmpleHarvest.util.dataBroker', 
{
    extend	: 'Ext.util.Observable',
    listeners		:
	{
    	filterResponse	: function(obj_response, str_obj_type)
		{
			this.filterData(obj_response, str_obj_type);
		},
		
		responseFailure	: function(e, obj)
		{
//			console.log('broker object not found');
//			console.log(obj.getId());
			Ext.destroy(obj);
		},
		
		responseSuccess	: function(obj)
		{
			obj.fireEvent('brokerResponse', this.obj_return);
		}
	},
	
	constructor	: function(config)
	{
		this.addEvents('filterResponse', 'responseFailure', 'responseSuccess');
		this.callParent();
	},
	

	brokerData	: function(obj_caller, str_id, str_obj_type)
	{
//		console.log(str_obj_type);
		if(!Ext.isObject(this.obj_data))
		{
			this.obj_data = new Object();
			this.getData(obj_caller, str_id, str_obj_type);
		}
		else
		{
			this.fireEvent('filterResponse', this.obj_data, str_obj_type);
		}
	},
	
	getData	: function(obj_caller, str_id, str_obj_type)
	{
		Ext.Ajax.request(
				{
					url		: obj_config.C_STR_EXTERNAL_PATH+'/rest/data/data.php',
					method	: 'GET',
					params	:
					{
						id		: str_id
					},
					timeout	: 100000,
					scope	: this,
					success	: function(response)
					{
//						try
//						{
							this.obj_data = Ext.decode(response.responseText);
							this.filterData(obj_caller, str_obj_type)
//						}
//						catch(e)
//						{
//							console.log('catch error');
//							console.log(e);
//						}
					},
					failure	: function(e)
					{
						console.log('Ajax Error:');
						console.log(e);
					}
				});
	},
	
	filterData	: function(obj, str_obj_type)
	{			
		this.obj_return = new Object();
		this.success = true;
		switch (str_obj_type)
		{
			case 'ccontent':
				if(Ext.isString(this.obj_data.content))
				{
					this.obj_return = { 
						content 	: this.obj_data.content,
						title		: this.obj_data.title,
						language	: this.obj_data.language,
						classification	: this.obj_data.classification//"SECRET",//
					}
				} else {
					this.returnNotFound(str_obj_type, obj);
				}
				break;
			case 'cmetadata':
				this.obj_return = {
						classification	: this.obj_data.classification,//"CONFIDENTIAL//FOR OFFICIAL USE ONLY",//
						data : [
							{ 
								name 	: 'Harvest Date',
								value	: this.obj_data.harvestDate
							},
							{ 
								name 	: 'Original ID',
								value	: this.obj_data.origId
							},
							{ 
								name 	: 'Source',
								value	: this.obj_data.source
							},
							{ 
								name 	: 'Language',
								value	: this.obj_data.language
							},
							{ 
								name 	: 'ID',
								value	: this.obj_data.id
							}
							]
						};
				break;
			// ENHANCEMENT OBJECTS
			case 'ampleharvest-objects-cce-ccce':
				if(Ext.isObject(this.obj_data.enhancement)&&Ext.isObject(this.obj_data.enhancement.CCE))
				{
					if(this.obj_data.enhancement.CCE.CCEEntities.length > 0)
					{
						this.obj_return = {
							classification	: this.obj_data.classification,//"TOPSECRET",//
							data : this.obj_data.enhancement.CCE
							}
						break;
					}	
				}
				
				this.returnNotFound(str_obj_type, obj);
				break;
			case 'metadata':
				if(Ext.isObject(this.obj_data.enhancement)&&Ext.isArray(this.obj_data.enhancement.metadata)&&(this.obj_data.enhancement.metadata.length > 0))
				{
					if(this.obj_data.enhancement.metadata.length > 0)
					{
						this.obj_return = {
								classification	: this.obj_data.classification,//"SECRET//FOUO",//
								data : this.obj_data.enhancement.metadata
								}
						break;
					}
				}
				this.returnNotFound(str_obj_type, obj);
				break;
			case 'ampleharvest-objects-breadcrumbs-cbreadcrumbs':
				if(Ext.isObject(this.obj_data.navigation)&&Ext.isObject(this.obj_data.navigation.breadcrumb))
				{
					this.obj_return = {
							classification	: this.obj_data.classification,//"CONFIDENTIAL//FOUO",//
							data : this.obj_data.navigation.breadcrumb
							}
					break;
				}
				this.returnNotFound(str_obj_type, obj);
				break;
			case 'ampleharvest-objects-breadcrumbs-cgotoparent':
				if(Ext.isObject(this.obj_data.navigation)&&Ext.isObject(this.obj_data.navigation.breadcrumb))
				{
					this.obj_return = {
							classification	: this.obj_data.classification,//"CONFIDENTIAL//FOUO",//
							data : this.obj_data.navigation.breadcrumb
							}
					break;
				}
				this.returnNotFound(str_obj_type, obj);
				break;
			case 'ampleharvest-objects-paging-cpages':
				if(Ext.isObject(this.obj_data.navigation)&&Ext.isArray(this.obj_data.navigation.pages)&&(this.obj_data.navigation.pages.length > 0))
				{
					this.obj_return = {
							classification	: this.obj_data.classification,//"CONFIDENTIAL//FOUO",//
							data : this.obj_data.navigation.pages
							}
					break
				}
				this.returnNotFound(str_obj_type, obj);
				break;
			case 'ampleharvest-objects-files-cfiles':
				if(Ext.isObject(this.obj_data.navigation)&&Ext.isArray(this.obj_data.navigation.attachments)&&(this.obj_data.navigation.attachments.length > 0))
				{
					this.obj_return = {
							classification	: this.obj_data.classification,//"CONFIDENTIAL//FOUO",//
							data : this.obj_data.navigation.attachments
							}
					break;
				}
				this.returnNotFound(str_obj_type, obj);
				break;	
			default:
				this.returnNotFound(str_obj_type, obj);
		}
		if(this.success)
		{
			this.fireEvent('responseSuccess', obj);
		}
		
	},
	
	returnNotFound	: function(str_obj_type, obj)
	{
		this.success = false;
		this.fireEvent('responseFailure', 'Object Type ['+str_obj_type+'] not found', obj);
	}

});/**
 * 
 */